<?php
/**
 * MobileController is the base class of mobile module.
 * 
 * LBB Solutions Ltd. 版权所有 @ 2013.
 */
class MobileController extends CController
{
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
	
	/**
	 * @var array the header is to set the mata header data.
	 */
	public $header = array();
	
	/**
	 * Error handler
	 */
	public function actionError() {
		$error = Yii::app()->errorHandler->error;
		if ($error) {
	    	if (Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

	/**
	 * Init all controllers
	 */
	public function init() {
		parent::init();
		
		//set the header info
		$this->header = array(
			'keywords' => BUtils::T('meta_keywords'),
			'title' => BUtils::T('meta_title'),
			'description' => BUtils::T('meta_description'),
			'image' => 'http://yamii.fi/img/logo.icon.114.png',
			'url' => Yii::app()->params['siteRoot'] . Yii::app()->request->url,
		);
	}
	
	protected function _sendJSON($status, $message, $end = true) {
		$json_string = array(
			'code' => $status,
			'message' => $message,
		);
		echo CJSON::encode($json_string);
		if ($end) Yii::app()->end();
	}
	
	/**
	 * Get the recent day name: today, tomorrow, Tus.
	 * @param string $weekdays 1,2,3,4,5,6,7
	 * @return string
	 */
	protected static function GetRecentDayName($weekdays, $week_string, $next24, $start_hour) {
		$today_lbl = BUtils::T('today_lbl');
		$tomorrow_lbl = BUtils::T('tomorrow_lbl');
		
		$today_weekno = date('N');
		$tomorrow_weekno = $today_weekno + 1;
		if ($today_weekno == 7) $tomorrow_weekno = 1;
		
		if ($next24) {
			if ($start_hour > date('Hi')) {// the start hour is coming
				$ipos = strpos($weekdays, $today_weekno);
				if ($ipos !== false) {
					return $today_lbl;//find today
				} else {
					$ipos = strpos($weekdays, (string)$tomorrow_weekno);
					if ($ipos !== false) {
						return $tomorrow_lbl;//find tomorrow
					}
				}
			} else {
				$ipos = strpos($weekdays, (string)$tomorrow_weekno);
				if ($ipos !== false) {
					return $tomorrow_lbl;//find tomorrow
				}
			}
		} else {
			$ipos = strpos($weekdays, $today_weekno);
			if ($ipos !== false) {
				return $today_lbl;//find today
			} else {
				$ipos = strpos($weekdays, (string)$tomorrow_weekno);
				if ($ipos !== false) {
					return $tomorrow_lbl;//find tomorrow
				}
			}
		}
		//return week days string
		return $week_string;
	}
	
	/**
	 * Return string with weekdays.
	 * @param string $weekdays 1,2,3 | 1,2,3,4,5 | 3,4 | 5
	 */
	protected static function GetWeekdays($weekdays, $week_array) {
		$array = explode(',', $weekdays);
		
		//only one day here
		if (count($array) == 1) {
			if (isset($week_array[$array[0]])) {
				return $week_array[$array[0]];
			} else {
				return null;
			}
		}
		
		//check if there is no break day
		$isbreak = false;
		$begin = $array[0];
		foreach ($array as $day) {
			if ($begin != $day) {
				$isbreak = true;
				break;
			}
			$begin ++;
		}
		$selected_days = null;
		if ($isbreak) {
			foreach ($array as $day) {
				if (isset($week_array[$day])) $selected_days .= "{$week_array[$day]} ";
			}
		} else {
			$selected_days = $week_array[$array[0]] . ' - ' . $week_array[$array[count($array)-1]];
		}
		
		return trim($selected_days);
	}
}